package org.zsk.cs.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zsk.cs.domain.po.HotVideo;
import org.zsk.cs.domain.vo.HotVideoVo;
import org.zsk.cs.response.ResultBody;
import org.zsk.cs.service.HotVideoService;
import org.zsk.fastdev.assign.Assign;

import java.util.List;

/**
 * <p>
 * 热点视频 controller
 * </p>
 *
 * @author zsk
 * @version 1.0
 * Created on 2020/03/29
 */
@RestController
@RequestMapping("/hotvideo")
public class HotVideoController {

    private final HotVideoService hotVideoService;
    private final Assign assign;

    @Autowired
    public HotVideoController(HotVideoService hotVideoService, Assign assign) {
        this.hotVideoService = hotVideoService;
        this.assign = assign;
    }

    @PostMapping("/a")
    public ResultBody add(@RequestBody HotVideoVo hotVideoVo) throws Exception {
        HotVideo hotVideo=new HotVideo();
        assign.assign(hotVideoVo,hotVideo);
        hotVideoService.save(hotVideo);
        return ResultBody.success();
    }

    @PutMapping("/u")
    public ResultBody update(@RequestBody HotVideoVo hotVideoVo) throws Exception {
        HotVideo hotVideo=new HotVideo();
        assign.assign(hotVideoVo,hotVideo);
        hotVideoService.updateById(hotVideo);
        return ResultBody.success();
    }

    @DeleteMapping("/d/{ids}")
    public ResultBody delete(@PathVariable List<Integer> ids) throws Exception {
        hotVideoService.removeByIds(ids);
        return ResultBody.success();
    }

    @GetMapping("/g")
    public ResultBody page(HotVideoVo hotVideoVo) throws Exception {
        Page<HotVideo> page=new Page<>();
        assign.assign(hotVideoVo,page,"$current,$size");
        return ResultBody.success(hotVideoService.page(page));
    }
}
